﻿using System;
using System.Xml.Linq;
using System.Drawing;
using System.Net;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using Net.Chirash.Twitter.TwitterDatabase;

namespace Net.Chirash.Twitter.TwitterClient
{
    public static partial class ParseExtends
    {
        //#region Public Properties

        //#region public long ID { get; set; }
        //private long _ID;
        //public long ID
        //{
        //    get { return _ID; }
        //    set { _ID = value; OnPropertyChanged("ID"); }
        //}
        //#endregion

        //#region public string Name { get; set; }
        //private string _Name;
        //public string Name
        //{
        //    get { return _Name; }
        //    set { _Name = value; OnPropertyChanged("Name"); }
        //}
        //#endregion

        //#region public string ScreenName { get; set; }
        //private string _ScreenName;
        //public string ScreenName
        //{
        //    get { return _ScreenName; }
        //    set { _ScreenName = value; OnPropertyChanged("ScreenName"); }
        //}
        //#endregion

        //#region public string Location { get; set; }
        //private string _Location;
        //public string Location
        //{
        //    get { return _Location; }
        //    set { _Location = value; OnPropertyChanged("Location"); }
        //}
        //#endregion

        //#region public string Description { get; set; }
        //private string _Description;
        //public string Description
        //{
        //    get { return _Description; }
        //    set { _Description = value; OnPropertyChanged("Description"); }
        //}
        //#endregion

        //#region public string ProfileImageUrl { get; set; }
        //private string _ProfileImageUrl;
        //public string ProfileImageUrl
        //{
        //    get { return _ProfileImageUrl; }
        //    set { _ProfileImageUrl = value; OnPropertyChanged("ProfileImageUrl"); }
        //}
        //#endregion

        //#region public string Url { get; set; }
        //private string _Url;
        //public string Url
        //{
        //    get { return _Url; }
        //    set { _Url = value; OnPropertyChanged("Url"); }
        //}
        //#endregion

        //#region public bool IsProtected { get; set; }
        //private bool _IsProtected;
        //public bool IsProtected
        //{
        //    get { return _IsProtected; }
        //    set { _IsProtected = value; OnPropertyChanged("IsProtected"); }
        //}
        //#endregion

        //#region public long FollowersCount { get; set; }
        //private long _FollowersCount;
        //public long FollowersCount
        //{
        //    get { return _FollowersCount; }
        //    set { _FollowersCount = value; OnPropertyChanged("FollowersCount"); }
        //}
        //#endregion

        //#region public long FriendsCount { get; set; }
        //private long _FriendsCount;
        //public long FriendsCount
        //{
        //    get { return _FriendsCount; }
        //    set { _FriendsCount = value; OnPropertyChanged("FriendsCount"); }
        //}
        //#endregion

        //#region public DateTime CreatedAt { get; set; }
        //private DateTime _CreatedAt;
        //public DateTime CreatedAt
        //{
        //    get { return _CreatedAt; }
        //    set { _CreatedAt = value; OnPropertyChanged("CreatedAt"); }
        //}
        //#endregion

        //#region public long FavouritesCount { get; set; }
        //private long _FavouritesCount;
        //public long FavouritesCount
        //{
        //    get { return _FavouritesCount; }
        //    set { _FavouritesCount = value; OnPropertyChanged("FavouritesCount"); }
        //}
        //#endregion

        //#region public string TimeZone { get; set; }
        //private string _TimeZone;
        //public string TimeZone
        //{
        //    get { return _TimeZone; }
        //    set { _TimeZone = value; OnPropertyChanged("TimeZone"); }
        //}
        //#endregion

        //#region public Color ProfileBackgroundColor { get; set; }
        //private Color _ProfileBackgroundColor;
        //public Color ProfileBackgroundColor
        //{
        //    get { return _ProfileBackgroundColor; }
        //    set { _ProfileBackgroundColor = value; OnPropertyChanged("ProfileBackgroundColor"); }
        //}
        //#endregion

        //#region public Color ProfileTextColor { get; set; }
        //private Color _ProfileTextColor;
        //public Color ProfileTextColor
        //{
        //    get { return _ProfileTextColor; }
        //    set { _ProfileTextColor = value; OnPropertyChanged("ProfileTextColor"); }
        //}
        //#endregion

        //#region public Color ProfileLinkColor { get; set; }
        //private Color _ProfileLinkColor;
        //public Color ProfileLinkColor
        //{
        //    get { return _ProfileLinkColor; }
        //    set { _ProfileLinkColor = value; OnPropertyChanged("ProfileLinkColor"); }
        //}
        //#endregion

        //#region public Color ProfileSideBarFillColor { get; set; }
        //private Color _ProfileSideBarFillColor;
        //public Color ProfileSideBarFillColor
        //{
        //    get { return _ProfileSideBarFillColor; }
        //    set { _ProfileSideBarFillColor = value; OnPropertyChanged("ProfileSideBarFillColor"); }
        //}
        //#endregion

        //#region public Color ProfileSideBarBorderColor { get; set; }
        //private Color _ProfileSideBarBorderColor;
        //public Color ProfileSideBarBorderColor
        //{
        //    get { return _ProfileSideBarBorderColor; }
        //    set { _ProfileSideBarBorderColor = value; OnPropertyChanged("ProfileSideBarBorderColor"); }
        //}
        //#endregion

        //#region public string Notifications { get; set; }
        //private string _Notifications;
        //public string Notifications
        //{
        //    get { return _Notifications; }
        //    set { _Notifications = value; OnPropertyChanged("Notifications"); }
        //}
        //#endregion

        //#region public string ProfileBackgroundImageUrl { get; set; }
        //private string _ProfileBackgroundImageUrl;
        //public string ProfileBackgroundImageUrl
        //{
        //    get { return _ProfileBackgroundImageUrl; }
        //    set { _ProfileBackgroundImageUrl = value; OnPropertyChanged("ProfileBackgroundImageUrl"); }
        //}
        //#endregion

        //#region public bool ProfileUseBackgroundImage { get; set; }
        //private bool _ProfileUseBackgroundImage;
        //public bool ProfileUseBackgroundImage
        //{
        //    get { return _ProfileUseBackgroundImage; }
        //    set { _ProfileUseBackgroundImage = value; OnPropertyChanged("ProfileUseBackgroundImage"); }
        //}
        //#endregion

        //#region public bool ProfileBackgroundTile { get; set; }
        //private bool _ProfileBackgroundTile;
        //public bool ProfileBackgroundTile
        //{
        //    get { return _ProfileBackgroundTile; }
        //    set { _ProfileBackgroundTile = value; OnPropertyChanged("ProfileBackgroundTile"); }
        //}
        //#endregion

        //#region public bool IsGeoEnabled { get; set; }
        //private bool _IsGeoEnabled;
        //public bool IsGeoEnabled
        //{
        //    get { return _IsGeoEnabled; }
        //    set { _IsGeoEnabled = value; OnPropertyChanged("IsGeoEnabled"); }
        //}
        //#endregion

        //#region public bool IsVerified { get; set; }
        //private bool _IsVerified;
        //public bool IsVerified
        //{
        //    get { return _IsVerified; }
        //    set { _IsVerified = value; OnPropertyChanged("IsVerified"); }
        //}
        //#endregion

        //#region public bool IsFollowing { get; set; }
        //private bool _IsFollowing;
        //public bool IsFollowing
        //{
        //    get { return _IsFollowing; }
        //    set { _IsFollowing = value; OnPropertyChanged("IsFollowing"); }
        //}
        //#endregion

        //#region public long StatusesCount { get; set; }
        //private long _StatusesCount;
        //public long StatusesCount
        //{
        //    get { return _StatusesCount; }
        //    set { _StatusesCount = value; OnPropertyChanged("StatusesCount"); }
        //}
        //#endregion

        //#region public string Language { get; set; }
        //private string _Language;
        //public string Language
        //{
        //    get { return _Language; }
        //    set { _Language = value; OnPropertyChanged("Language"); }
        //}
        //#endregion

        //#region public bool IsContributorsEnabled { get; set; }
        //private bool _IsContributorsEnabled;
        //public bool IsContributorsEnabled
        //{
        //    get { return _IsContributorsEnabled; }
        //    set { _IsContributorsEnabled = value; OnPropertyChanged("IsContributorsEnabled"); }
        //}
        //#endregion


        //#endregion

        #region Public Static Methods

        /// <summary>
        /// Parse XML text for User
        /// </summary>
        /// <param name="xml">User XML text to parse</param>
        /// <returns>User</returns>
        public static User Parse(this User user, string xml)
        {
            //TODO: Exception
            XElement element = XElement.Parse(xml);
            //User user = new User();

            user.UserID = Convert.ToInt64(element.Element("id").Value);

            //if (element.Elements().Count() == 1) //For TrimUser Option
            //{
            //    user.IsTrimed = true;
            //}
            //else
            //{
                user.Name = WebUtility.HtmlDecode(element.Element("name").Value);

                user.ScreenName = WebUtility.HtmlDecode(element.Element("screen_name").Value);

                user.Location = WebUtility.HtmlDecode(element.Element("location").Value);

                user.Description = WebUtility.HtmlDecode(element.Element("description").Value);

                user.ProfileImageUrl = element.Element("profile_image_url").Value;

                user.Url = element.Element("url").Value;

                user.IsProtected = bool.Parse(element.Element("protected").Value);

                user.FollowersCount = Convert.ToInt64(element.Element("followers_count").Value);

                user.FriendsCount = Convert.ToInt64(element.Element("friends_count").Value);

                user.CreatedAt = DateTime.ParseExact(element.Element("created_at").Value, "ddd MMM d HH':'mm':'ss zzz yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo);//

                string utc_offset = element.Element("utc_offset").Value;
                if (!string.IsNullOrEmpty(utc_offset))
                {
                    user.CreatedAt.AddSeconds(Convert.ToInt32(utc_offset));
                }

                user.FavouritesCount = Convert.ToInt64(element.Element("favourites_count").Value);

                user.TimeZone = WebUtility.HtmlDecode(element.Element("time_zone").Value);

                string profile_background_color = element.Element("profile_background_color").Value;
                if (!string.IsNullOrWhiteSpace(profile_background_color))
                {
                    user.ProfileBackgroundColor = ColorTranslator.FromHtml("0x" + element.Element("profile_background_color").Value).ToArgb();
                }

                string profile_text_color = element.Element("profile_text_color").Value;
                if (!string.IsNullOrWhiteSpace(profile_text_color))
                {
                    user.ProfileTextColor = ColorTranslator.FromHtml("0x" + element.Element("profile_text_color").Value).ToArgb();
                }

                string profile_link_color = element.Element("profile_link_color").Value;
                if (!string.IsNullOrWhiteSpace(profile_text_color))
                {
                    user.ProfileLinkColor = ColorTranslator.FromHtml("0x" + element.Element("profile_link_color").Value).ToArgb();
                }

                string profile_sidebar_fill_color = element.Element("profile_sidebar_fill_color").Value;
                if (!string.IsNullOrWhiteSpace(profile_sidebar_fill_color))
                {
                    user.ProfileSideBarFillColor = ColorTranslator.FromHtml("0x" + profile_sidebar_fill_color).ToArgb();
                }

                string profile_sidebar_border_color = element.Element("profile_sidebar_border_color").Value;
                if (!string.IsNullOrWhiteSpace(profile_sidebar_border_color))
                {
                    user.ProfileSideBarBorderColor = ColorTranslator.FromHtml("0x" + profile_sidebar_border_color).ToArgb();
                }

                bool profile_use_background_image;
                bool.TryParse(element.Element("profile_use_background_image").Value, out profile_use_background_image);
                user.ProfileUseBackgroundImage = profile_use_background_image;
                if (user.ProfileUseBackgroundImage)
                {
                    user.ProfileBackgroundImageUrl = element.Element("profile_background_image_url").Value;
                }
                else
                {
                    user.ProfileBackgroundImageUrl = "";
                }

                user.ProfileBackgroundTile = bool.Parse(element.Element("profile_background_tile").Value);

                user.StatusesCount = Convert.ToInt64(element.Element("statuses_count").Value);

                user.Language = element.Element("lang").Value;

                user.Notifications = WebUtility.HtmlDecode(element.Element("notifications").Value);

                bool geo_enabled;
                bool.TryParse(element.Element("geo_enabled").Value, out geo_enabled);
                user.IsGeoEnabled = geo_enabled;

                bool isVerified;
                bool.TryParse(element.Element("verified").Value, out isVerified);
                user.IsVerified = isVerified;

                bool isFollowing;
                bool.TryParse(element.Element("following").Value, out isFollowing);
                user.IsFollowing = isFollowing;
            //}
            return user;
        }

        #endregion
    }
}
